const app = require('../server')
const Group = require('../../model/Group/index')
const validate = require('validate.js')

const constraints = {
    name: {
        type: 'string',
        presence: {
            allowEmpty: false,
            // message:'不能为空'
        }
    }
}
const whiteList = {
    name: true
}
// app.get('')
// 创建团队
exports.addGroup = async function (data) {
    let res = null
    try {
        data = validate.cleanAttributes(data, whiteList)
        await validate.async(data, constraints)
        res = await Group.create(data)
    } catch (e) {
        return e
    }
    return res.toJSON()

}

// 修改团队名称
exports.updateGroup = async function (id, data) {
    if (validate.isDefined(id) && validate.isEmpty(id)) {
        return '{id:id can not be empty}'
    }

    let res = null
    try {
        data = validate.cleanAttributes(data, whiteList)
        await validate.async(data, constraints)
        res = await Group.update(data, {
            where: {
                id
            }
        })
    } catch (e) {
        return e
    }
    return JSON.stringify(res)
}

// 删除团队